// pages/login/login.js
const app = getApp()
var http = app.globalData.http
Page({

  /**
   * 页面的初始数据
   */
    data: {
        phone: "",
        code: "",
        codeMsg:"获取验证码",
        disabled:false,
        disabledone:true,
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var accountid = options.accountid
      this.setData({
          accountid: accountid
      })
  },
    bindPhone:function(e){
        // console.log(e)
        var phone = e.detail.value;
        this.setData({
            phone: phone
        })
    },
    bindCode:function(e){
        var code = e.detail.value;
        this.setData({
            code: code
        })
    },
    /**
     * 获取验证码
     */
    getCode:function(){
        var that = this
        var phone = that.data.phone//获取手机号
        var phoneV = checkMobile(phone)
        if (phoneV == false) {
            return false
        }      
        this.setData({
          disabled: true,
          disabledone: false,
        })
        var codeMsg = this.data.codeMsg
        if (codeMsg == "重新获取" || codeMsg == "获取验证码"){
            let time = 60;
            let clearTime = setInterval(() => {
                if (time != 0) {
                    time--
                    this.setData({
                        codeMsg: time + '秒'
                    })
                } else {
                    clearInterval(clearTime);
                    this.setData({
                        codeMsg: '重新获取'

                    })
                }
            }, 1000)
            wx.request({
                url: http + '/rest/validate/sendCode.do', //获取验证码
                method: "GET",
                data: {
                    mobile: phone,
                    codeType: 1006
                },
                header: {
                    'content-type': 'application/x-www-form-urlencoded', // 默认值
                    'X-Sign': "",
                    'X-Time': (new Date()).valueOf(),
                    'X-Ticket': "",
                },
                dataType: "JSON",
                success: function (res) {
                    var obj = JSON.parse(res.data);
                    console.log(obj);
                    if (obj.code == 0) {

                    } else {
                        console.log(obj.msg)
                        wx.showToast({
                            title: obj.msg,
                            icon: "none",
                            duration: 2000,
                            mask: true
                        })
                    }
                }
            })
        }else{

        }
    
    },
    /**
     * 绑定手机号提交按钮
     */
    onGotUserInfo:function(){
        var that = this
        var phone = that.data.phone  //获取手机号
        var code = that.data.code   //获取验证码
        var verification = that.Verification(phone,code)
        console.log(verification)
        if (verification == true){
            console.log("准备提交")
            var accountid = that.data.accountid
            wx.request({
                url: http + '/rest/weixin/valitateCode.do', //首页banner轮播
                method: "GET",
                data:{
                    mobile: phone,
                    code: code,
                    accountid: accountid ,
                },
                header: {
                    'content-type': 'application/x-www-form-urlencoded', // 默认值
                    'X-Sign': "",
                    'X-Time': (new Date()).valueOf(),
                    'X-Ticket': "",
                },
                dataType: "JSON",
                success: function (res) {
                    var obj = JSON.parse(res.data);  
                    if (obj.code == 0) {
                        console.log(obj);
                        app.globalData.user = obj.data.accountDto.account  //保存用户信息
                        app.globalData.ticket = obj.data.ticket   //保存ticket
                        wx.showToast({
                            title:"登录成功",
                            icon: "success",
                            duration: 2000,
                            mask: true,
                            success:function(){
                                wx.switchTab({
                                    url: '/pages/personal_center/personal_center'
                                })
                            }
                        })
                    } else {
                        console.log(obj.msg)
                        wx.showToast({
                            title: obj.msg,
                            icon: "none",
                            duration: 2000,
                            mask: true
                        })
                    }
                }
            })

        }
    },
    Verification(phone,code){
        var phone = checkMobile(phone)  //返回true 或者 false
        var code = code.length==6?true:false
        if (phone==false){
            return "请输入正确手机号"
        }
        if (code==false){
            return "请输入正确的验证码"
        }
        return true
    },
})
/**
 * 验证手机号
 */
function checkMobile(phone) {
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(phone)) {
        return false;
    } else {
        return true;
    }
}